// DialogOption.cpp : implementation file
//

#include "stdafx.h"
#include "map863.h"
#include "DialogOption.h"
#include "MainOption.h"


// CDialogOption dialog

IMPLEMENT_DYNAMIC(CDialogOption, CDialog)

CDialogOption::CDialogOption(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogOption::IDD, pParent)
{

		DialogSetPath.Create(IDD_DIALOG_OPTION_PATH2, this);		
//		DialogSetPath.ModifyStyle(WS_CHILD, 0);
//		DialogSetPath.SetParent(this);

}

CDialogOption::~CDialogOption()
{	
	DialogSetColor.DestroyWindow();
}

void CDialogOption::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE1, m_Tree1);
	DDX_Control(pDX, IDC_OPTIONTEXT, m_OptionText);
	DDX_Control(pDX, IDC_STATIC_OPTIONAREA, m_OptionArea);
}


BEGIN_MESSAGE_MAP(CDialogOption, CDialog)
	ON_BN_CLICKED(IDC_BUTTON1, &CDialogOption::OnBnClickedButton1)
	ON_NOTIFY(NM_CLICK, IDC_TREE1, &CDialogOption::OnNMClickTree1)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CDialogOption::OnTvnSelchangedTree1)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()


// CDialogOption message handlers

BOOL CDialogOption::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	m_Tree1.ModifyStyleEx(0, WS_EX_CLIENTEDGE|TVS_HASBUTTONS |TVS_HASLINES );
//	m_Tree1.SetBkColor(0x00E0E0C0);
	HTREEITEM TreeNode1 = m_Tree1.InsertItem(C_MAINOPTION1);
	m_Tree1.Expand ( TreeNode1,TVE_EXPAND   );

	HTREEITEM TreeNode2 = m_Tree1.InsertItem(C_MAINOPTION2);
//	m_Tree1.InsertItem(C_MAINOPTION21, TreeNode2);
	m_Tree1.Expand ( TreeNode2,TVE_EXPAND   );

	HTREEITEM TreeNode3 = m_Tree1.InsertItem(C_MAINOPTION3);
	m_Tree1.Expand ( TreeNode3,TVE_EXPAND   );

	HTREEITEM TreeNode4 = m_Tree1.InsertItem(C_MAINOPTION4);
	m_Tree1.Expand ( TreeNode4,TVE_EXPAND   );


//	hwndOptionDialog=CreateWindow(CDialogSetColor,_T("123"), WS_CHILD, 110, 7, 192, 170,
//									this, NULL, hInstance, NULL);
//	HWND hDialog = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOGSETCOLOR), NULL, (DLGPROC)MyDialogProc);   

//	ShowWindow(hwndOptionDialog);
/*
	DialogSetColor.Create(IDD_DIALOG_OPTION_COLOR, this);
	DialogSetColor.ModifyStyle(WS_CHILD, 0);
	DialogSetColor.SetParent(this);
	CRect rect1,rect2;
	GetWindowRect(&rect1);
	int x,y;
	CWnd * hwnd_OptionArea;
	hwnd_OptionArea=GetDlgItem(IDC_STATIC_OPTIONAREA);
	hwnd_OptionArea->GetWindowRect(&rect2);

	x=rect2.left-rect1.left;
	y=rect2.top-rect1.top;
//	x=110;y=7;
	DialogSetColor.MoveWindow(x, y, rect2.Width() , rect2.Height());
	TRACE("%d, %d, %d, %d \n",x, y, rect2.Width() , rect2.Height());
	DialogSetColor.ShowWindow(SW_SHOW);
*/
	SetTitleText();
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogOption::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here


}

void CDialogOption::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: Add your control notification handler code here
	*pResult = 0;
//	MessageBox(_T(" single click tree!"));
}

void CDialogOption::OnTvnSelchangedTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	// TODO: Add your control notification handler code here

	HTREEITEM CurrentSelectedOption=m_Tree1.GetSelectedItem();

//	if (TextofSelectedOption==C_MAINOPTION21)
//		MessageBox(_T(" tree!"));

	CRect rect2;
	int x,y;
	m_OptionArea.GetWindowRect(&rect2);
	ScreenToClient(&rect2);
	x=rect2.left+10;
	y=rect2.top+10;
	RemoveSubDialog();

  	CString TextofSelectedOption=m_Tree1.GetItemText (CurrentSelectedOption);

	if (TextofSelectedOption==C_MAINOPTION2)
		ShowDialogSetColor(x, y, rect2.Width(), rect2.Height());
//	else if (TextofSelectedOption==C_MAINOPTION22)
//		ShowDialogSetColor(x, y, rect2.Width(), rect2.Height());
	else if (TextofSelectedOption==C_MAINOPTION3)
		ShowDialogSetPath(x, y, rect2.Width(), rect2.Height());	
	else if (TextofSelectedOption==C_MAINOPTION4)
		ShowDialogSetBG(x, y, rect2.Width(), rect2.Height());

//	CDC * pDC=GetDC();
//	pDC->MoveTo(x, y-20);
//	pDC->LineTo(x+rect2.Width(), y-20);
	
//	pDC->MoveTo(x,TreeRect->bottom) ;
//	pDC->LineTo(x+rect2.Width(), TreeRect->bottom);

	CFont OptionTextFont;
	OptionTextFont.CreateFont(
	24,                        // nHeight
	0,                         // nWidth
	0,                         // nEscapement
	0,                         // nOrientation
	FW_NORMAL,                 // nWeight
	FALSE,                     // bItalic
	FALSE,                     // bUnderline
	0,                         // cStrikeOut
	ANSI_CHARSET,              // nCharSet
	OUT_DEFAULT_PRECIS,        // nOutPrecision
	CLIP_DEFAULT_PRECIS,       // nClipPrecision
	DEFAULT_QUALITY,           // nQuality
	DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
	_T("Arial"));                 // lpszFacename

	m_OptionText.SetFont(&OptionTextFont);
	m_OptionText.SetWindowText(TextofSelectedOption);

	*pResult = 0;
}


bool CDialogOption::RemoveSubDialog(void)
{
	DialogSetColor.DestroyWindow();
	DialogSetPath.DestroyWindow();
	DialogSetBG.DestroyWindow();

	return true;
}

bool CDialogOption::ShowDialogSetColor(int x, int y, int Width, int Height)
{
	DialogSetColor.Create(IDD_DIALOG_OPTION_COLOR, this);
	DialogSetColor.ModifyStyle(WS_CHILD, 0);
	DialogSetColor.SetParent(this);
	DialogSetColor.MoveWindow(x, y, Width, Height);
	DialogSetColor.ShowWindow(SW_SHOW);
	
	return false;
}

bool CDialogOption::ShowDialogSetPath(int x, int y, int Width, int Height)
{
	DialogSetPath.Create(IDD_DIALOG_OPTION_PATH2, this);
	DialogSetPath.ModifyStyle(WS_CHILD, 0);
	DialogSetPath.SetParent(this);
	DialogSetPath.MoveWindow(x, y, Width, Height);
	DialogSetPath.ShowWindow(SW_SHOW);
	
	return false;
}

bool CDialogOption::ShowDialogSetBG(int x, int y, int Width, int Height)
{
	DialogSetBG.Create(IDD_DIALOG_OPTION_BG, this);
	DialogSetBG.ModifyStyle(WS_CHILD, 0);
	DialogSetBG.SetParent(this);
	DialogSetBG.MoveWindow(x, y, Width, Height);
	DialogSetBG.ShowWindow(SW_SHOW);
	
	return false;
}


bool CDialogOption::SetTitleText(void)
{
	CFont OptionTextFont;
	OptionTextFont.CreateFont(
	24,                        // nHeight
	0,                         // nWidth
	0,                         // nEscapement
	0,                         // nOrientation
	FW_NORMAL,                 // nWeight
	FALSE,                     // bItalic
	FALSE,                     // bUnderline
	0,                         // cStrikeOut
	ANSI_CHARSET,              // nCharSet
	OUT_DEFAULT_PRECIS,        // nOutPrecision
	CLIP_DEFAULT_PRECIS,       // nClipPrecision
	DEFAULT_QUALITY,           // nQuality
	DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
	_T("Arial"));                 // lpszFacename
	
	HTREEITEM CurrentSelectedOption=m_Tree1.GetSelectedItem();
	CString TextofSelectedOption=m_Tree1.GetItemText (CurrentSelectedOption);

	m_OptionText.SetFont(&OptionTextFont);
	m_OptionText.SetWindowText(TextofSelectedOption);

	return true;
}

BOOL CDialogOption::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default

	return CDialog::OnEraseBkgnd(pDC);
}
